<!--
 Copyright (C) 2004 by Alain Bienvenue. All rights reserved.
 Released under the terms of the GNU General Public License version 2 or later.
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>SpreadsheetTranslator Test</title>
</head>
<body>

<script language="JavaScript" type="text/javascript" src="jsUnitTestLink.js"></script>
<script language="javascript" type="text/javascript" src="SpreadsheetTranslator.js"></script>

<script language="JavaScript" type="text/javascript">

function setUp()
{
  translator = new SpreadsheetTranslator();
}

function testLineSizeNoCells()
{
  var line = new Array;
  assertEquals(0, translator.lineSize(line));
}

function testLineSize0()
{
  var line = new Array;
  line[0] = '';
  line[1] = '';
  assertEquals(0, translator.lineSize(line));
}

function testLineSize1()
{
  var line = new Array;
  line[0] = 'x';
  line[1] = '';
  assertEquals(1, translator.lineSize(line));
}

function testLineSize2()
{
  var line = new Array;
  line[0] = '';
  line[1] = 'x';
  line[2] = '';
  assertEquals(2, translator.lineSize(line));
}

function testLineSize3()
{
  var line = new Array;
  line[0] = '';
  line[1] = 'x';
  line[2] = 'x';
  assertEquals(3, translator.lineSize(line));
}

function testRemoveCarriageReturns()
{
  var before = "1\r\n2\r\n";
  var after = translator.removeCarriageReturns(before);
  assertEquals("1\n2\n", after);
}


function verifySpreadsheetToFitnesse(input, expected)
{
  translator.parseExcelTable(input);
  var output = translator.getFitNesseTables();
  assertEquals("first pass", expected, output);

  input = output;
  translator.parseExcelTable(input);
  output = translator.getFitNesseTables();
  assertEquals("second pass", expected, output);
}

function testSetupFixture()
{
  var input = "fit.Setup";
  var expected = "!|fit.Setup|";

  verifySpreadsheetToFitnesse(input, expected);
}


function testSetupFixtureAndComments()
{
  var input = "\Prefix comment\r\n" +
              "\r\n" +
              "package.Class\r\n" +
              "\r\n" +
              "Suffix Comment\r\n";
  var expected = "Prefix comment\n" +
                 "\n" +
                 "!|package.Class|\n" +
                 "\n" +
                 "Suffix Comment\n";

  verifySpreadsheetToFitnesse(input, expected);
}

function testExplicitTableAndComments()
{
  var input = "\Prefix comment\r\n" +
              "!\tpackage.Class\r\n" +
              "Suffix Comment\r\n";
  var expected = "Prefix comment\n" +
                 "!|package.Class|\n" +
                 "Suffix Comment\n";

  verifySpreadsheetToFitnesse(input, expected);
}

function testMultipleColumnsTable()
{
  var input = "fit.OneTest\t\t\r\n" +
              "input1\tinput2\toutput()\r\n" +
              "1\t2\t3\r\n" +
              "1\t2\t";
  var expected = "!|fit.OneTest|\n" +
                 "|input1|input2|output()|\n" +
                 "|1|2|3|\n" +
                 "|1|2||";

  verifySpreadsheetToFitnesse(input, expected);
}

function testExplicitTable()
{
  var input = "\r\n!\tfit.OneTest\t\t\r\n" +
              "\tinput1\tinput2\toutput()\r\n" +
              "\t1\t2\t3\r\n" +
              "\t1\t2\t";
  var expected = "\n!|fit.OneTest|\n" +
                 "|input1|input2|output()|\n" +
                 "|1|2|3|\n" +
                 "|1|2||";

  verifySpreadsheetToFitnesse(input, expected);
}

function testActionFixtureTable()
{
  var input = "\r\n!\tfit.ActionFixture\t\t\r\n" +
              "\tstart\tpackage.MyFixture\t\r\n" +
              "\tpress\tstart\t\r\n" +
              "\tcheck\tstarted\ttrue\r\n";
  var expected = "\n!|fit.ActionFixture|\n" +
                 "|start|package.MyFixture|\n" +
                 "|press|start|\n" +
                 "|check|started|true|\n";

  verifySpreadsheetToFitnesse(input, expected);
}

function testPlainTable()
{
  var input = "1\t2\t3\r\n" +
              "1\t2\t";
  var expected = "!|1|2|3|\n" +
                 "|1|2|";

  verifySpreadsheetToFitnesse(input, expected);
}

// this tests the case where a user converts wiki text to spreadsheet,
// and back to wiki. In this case, it is possible that not all lines contain
// as many cells as the longest line.
function testConvertBackUnevenTable()
{
  var input = "!\t1\t2\t3\r\n" +
              "\t1\t2\t3\r\n" +
              "\t1" ;
  var expected = "!|1|2|3|\n" +
                 "|1|2|3|\n" +
                 "|1|";

  verifySpreadsheetToFitnesse(input, expected);
}

function testGracefulNamesTable()
{
  var input = "One Test\t\r\n" +
              "input1\toutput()\r\n" +
              "1\t3";
  var expected = "!|One Test|\n" +
                 "|input1|output()|\n" +
                 "|1|3|";

  verifySpreadsheetToFitnesse(input, expected);
}

function testExplicitOneColumnnTable()
{
  var input = "!\tAdd Rows\t\t\r\n" +
              "\tone\t\t\r\n" +
              "\ttwo\t\t\r\n";
  var expected = "!|Add Rows|\n" +
                 "|one|\n" +
                 "|two|\n";

  verifySpreadsheetToFitnesse(input, expected);
}

// a test table is not escaped and translated to spreadsheet format
// the first column will be empty for all lines.
function testNotEscapedWikiSpreadsheetTable()
{
  var input = "\tfit.OneTest\t\t\r\n" +
              "\tinput1\tinput2\toutput()\r\n" +
              "\t1\t2\t3\r\n" +
              "\t1\t2\t";
  var expected = "!|fit.OneTest|\n" +
                 "|input1|input2|output()|\n" +
                 "|1|2|3|\n" +
                 "|1|2||";

  verifySpreadsheetToFitnesse(input, expected);
}


</script>
</body>
</html>
